package mediatriggerservice;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

public class MediaTriggerServiceConfig 
{
	public static final String GVIA_COMMON_CONFIG_FILE = "/etc/gvia/gviaConfig.properties"; 
	private static final String MEDIA_TRIGGER_SERVICE_CONFIG_FILE = "mediatriggerserviceConfig.properties";
	protected static MediaTriggerServiceConfig singleton = null;
	private Properties gviaCommonProps;
	private Properties configProperties;
	private String[] incomingEvents;
	private String jdbcConnectionURI;
	private String user;
	private String password;
	
	private MediaTriggerServiceConfig() throws Exception
	{
		InputStream is = null;
		InputStream gviaIs = null;
		try
		{
			configProperties = new Properties();
			is = this.getClass().getResourceAsStream(MEDIA_TRIGGER_SERVICE_CONFIG_FILE);
			if (is == null)
				throw new Exception(MEDIA_TRIGGER_SERVICE_CONFIG_FILE + " file not found");
			configProperties.load(is);
			is.close();
			gviaCommonProps = new Properties();
			gviaIs = new FileInputStream(GVIA_COMMON_CONFIG_FILE);
			gviaCommonProps.load(gviaIs);
			gviaIs.close();
			setPassword();
			setUser();
			setIncomingEvents();
			setJdbcConnectionURI();
		}
		catch(Exception e)
		{
			if (is != null)
				is.close();
			if (gviaIs != null)
				gviaIs.close();
			throw e;
		}
	}
	
	public static MediaTriggerServiceConfig getConfig() throws Exception 
	{
		try
		{
			if (singleton == null)
			{
				singleton = new MediaTriggerServiceConfig();
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return singleton;
	}
	
	public String getPassword() 
	{
		return password;
	}

	public void setPassword() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("password");
		if (propValue == null)
			throw new Exception("password not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			password = propValue;
	}

	public String getUser() 
	{
		return user;
	}

	public void setUser() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("user");
		if (propValue == null)
			throw new Exception("user not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			user = propValue;
	}

	private void setIncomingEvents() throws Exception
	{
		String propValue = configProperties.getProperty("incomingEvents");
		if (propValue == null)
		{
			throw new Exception("incomingEvents not added in " + MEDIA_TRIGGER_SERVICE_CONFIG_FILE + " file");
		}
		else
		{
			StringTokenizer st = new StringTokenizer(propValue, ",");
			ArrayList list = new ArrayList();
			while (st.hasMoreTokens())
			{
				list.add(st.nextToken());
			}
			incomingEvents = new String [list.size()];
			list.toArray (incomingEvents);
		}
	}
	
	public String[] getIncomingEvents() 
	{
		return incomingEvents;
	}

	private void setJdbcConnectionURI() throws Exception
	{
		String propValue = configProperties.getProperty("jdbcConnectionURI");
		if (propValue == null)
			throw new Exception("jdbcConnectionURI not added in " + MEDIA_TRIGGER_SERVICE_CONFIG_FILE + " file");
		else
			jdbcConnectionURI = propValue;
	}
	
	public String getJdbcConnectionURI()
	{
		return jdbcConnectionURI;
	}
}
